package com.efast.dmom.mes.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.mes.bean.PieceWage;
import com.efast.dmom.mes.bean.ProductRoute;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.RouteStep;
import com.efast.dmom.mes.dao.entity.PieceWageDao;
import com.efast.dmom.mes.datahandlers.entity.RouteDataHandler;
import com.efast.dmom.mes.entity.PieceWageEntity;
import com.efast.dmom.mes.entity.ProductRouteEntity;
import com.efast.dmom.mes.entity.RouteStepEntity;
import com.efast.dmom.mes.service.PieceWageService;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;

@Service
public class PieceWageServiceImpl implements PieceWageService {

	@Autowired
	private PieceWageDao pieceWageDao;

	@Autowired
	private RouteDataHandler routeDataHandler;

	@Override
	public List<PieceWageEntity> getPieceWageListByUomId(long uomId) {
		List<PieceWageEntity> pieceWageList = pieceWageDao.getPieceWageListByUomId(uomId);
		if (pieceWageList != null && pieceWageList.size() > 0) {
			return pieceWageList;
		}
		return null;
	}

	@Override
	public ResultBean deletePieceWage(PieceWage pieceWage) {
		ResultBean resultBean = new ResultBean(true, " ");
		PieceWageEntity pieceWageEntity = pieceWage.getInnerData();
		pieceWageEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		pieceWageEntity.setLastModifier(SpringWebUtils.getRequestUser());
		pieceWageEntity.setRowStatus(1);
		pieceWageDao.deletePieceWage(pieceWageEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("删除成功！");
		return resultBean;
	}

	@Override
	public ResultBean verifyPieceWageUniqued(PieceWage pieceWage) {
		ResultBean resultBean = new ResultBean(true, " ");
		PieceWageEntity pieceWageEntity = pieceWageDao.getPieceWageEntityByNameAndOperationId(pieceWage.getInnerData());
		if (pieceWageEntity != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("同一工序下的单件计件工资名称不可重复！");
			return resultBean;
		}
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public ResultBean save(PieceWage pieceWage) {
		ResultBean resultBean = new ResultBean(true, " ");
		PieceWageEntity pieceWageEntity = pieceWage.getInnerData();
		if (pieceWageEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("保存的信息不能为空！");
			return resultBean;
		}
		if (StringUtil.isEmpty(pieceWageEntity.getPieceWageName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("单件工资项目名称不能为空！");
			return resultBean;
		}
		if (pieceWageEntity.getOperationId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工序信息不能为空！");
			return resultBean;
		}
		if (pieceWageEntity.getUomId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("单位不能为空！");
			return resultBean;
		}
		PieceWageEntity pieceWageEntity2 = pieceWageDao.getPieceWageEntityByOperationId(pieceWageEntity);
		if (pieceWageEntity2 != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该工序下已存在数据！");
			return resultBean;
		}
		PieceWageEntity wageEntity = pieceWageDao.getPieceWageEntityByNameAndOperationId(pieceWageEntity);
		if (wageEntity != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("同一工序下的单件工资项目名称不可重复！");
			return resultBean;
		}
		pieceWageEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
		pieceWageEntity.setCreationTime(TimeUtil.getLocalDateTime());
		pieceWageEntity.setCreator(SpringWebUtils.getRequestUser());
		pieceWageEntity.setRowStatus(0);
		pieceWageDao.save(pieceWageEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("保存成功！");
		return resultBean;
	}

	@Override
	public ResultBean update(PieceWage pieceWage) {
		ResultBean resultBean = new ResultBean(true, " ");
		PieceWageEntity pieceWageEntity = pieceWage.getInnerData();
		if (pieceWageEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的信息不能为空！");
			return resultBean;
		}
		if (pieceWageEntity.getUomId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("单位不能为空！");
			return resultBean;
		}
		pieceWageEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		pieceWageEntity.setLastModifier(SpringWebUtils.getRequestUser());
		pieceWageDao.update(pieceWageEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("修改成功!");
		return resultBean;
	}

	@Override
	public ResultBean savePieceWage(ProductRoute productRoute) {
		ResultBean resultBean = new ResultBean(true, " ");
		if (productRoute == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空！");
			return resultBean;
		}

		ProductRouteEntity productRouteEntity = productRoute.getInnerData();
		// 获取该工艺路线的所有工序
		List<RouteStep> routeStepList = routeDataHandler.getAllRouteStepByRouteKey(productRouteEntity.getRouteId());
		List<PieceWageEntity> pieceWageList = new ArrayList<PieceWageEntity>();
		if (routeStepList != null && routeStepList.size() > 0) {
			for (RouteStep routeStep : routeStepList) {
				RouteStepEntity innerData = routeStep.getInnerData();
				PieceWageEntity pieceWageEntity = new PieceWageEntity();
				pieceWageEntity.setPieceWageName(productRouteEntity.getPartName() + "-" + innerData.getOperationName());
				pieceWageEntity.setOperationId(innerData.getOperationKey());
				pieceWageEntity.setRouteId(productRouteEntity.getRouteId());
				pieceWageEntity.setPartId(productRouteEntity.getPartId());
				pieceWageEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
				pieceWageEntity.setCreationTime(TimeUtil.getLocalDateTime());
				pieceWageEntity.setCreator(SpringWebUtils.getRequestUser());
				pieceWageEntity.setRowStatus(0);
				pieceWageList.add(pieceWageEntity);
			}
		}
		if (pieceWageList != null && pieceWageList.size() > 0) {
			int saveQty = pieceWageDao.saveBatch(pieceWageList);
			if (saveQty > 0) {
				resultBean.setMessage("piece wage save successed");
			} else {
				resultBean.setSuccess(false);
				resultBean.setMessage("piece wage save failed");
			}
		} else {
			resultBean.setMessage("no piece wage");
		}
		return resultBean;
	}

	@Override
	public ResultBean updatePieceWage(ProductRoute productRoute) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deletePieceWageByPartIdAndRouteId(ProductRoute productRoute) {
		PieceWageEntity pieceWageEntity = new PieceWageEntity();
		pieceWageEntity.setPartId(productRoute.getInnerData().getPartId());
		pieceWageEntity.setRouteId(productRoute.getInnerData().getRouteId());
		pieceWageEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		pieceWageEntity.setLastModifier(SpringWebUtils.getRequestUser());
		pieceWageEntity.setRowStatus(1);
		pieceWageDao.deletePieceWageByPartIdAndRouteId(pieceWageEntity);
	}

	@Override
	public List<PieceWageEntity> getPieceWageListByRouteIdAndPartId(ProductRoute productRoute) {
		List<PieceWageEntity> entityList = pieceWageDao.getPieceWageListByRouteIdAndPartId(
				productRoute.getInnerData().getPartId(), productRoute.getInnerData().getRouteId());
		return entityList;
	}

}
